import { OrderType } from '@/enums'
import { cancelOrder, deleteOrder, followTarget, getPrescriptionPic } from '@/services/consult'
import { getMedicalOrderDetail } from '@/services/order'
import { sendMobileCode } from '@/services/user'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import type { OrderDetail } from '@/types/order'
import type { CodeType } from '@/types/user'
import { showFailToast, showImagePreview, showSuccessToast, type FormInstance } from 'vant'
import { onMounted, onUnmounted, ref, type Ref } from 'vue'

export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    // 开始加载
    loading.value = true
    try {
      await followTarget(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      // 不管成功还是错误，直接执行
      loading.value = false
    }
  }
  // 方法和数据提出
  return { loading, follow }
}

// 查看处分
export const useShowPrescription = () => {
  const showPrescriptionPic = async (id?: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return { showPrescriptionPic }
}

// 取消订单
export const useCancelOrder = () => {
  const loading = ref(false)
  const onCancelOrder = async (item: ConsultOrderItem) => {
    loading.value = true
    try {
      await cancelOrder(item.id)
      // 修改订单状态
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (e) {
      showFailToast('取消失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, onCancelOrder }
}

// 删除订单 传入函数为参数实现功能
export const useDeleteOrder = (callblack: (id: string) => void) => {
  const loadingDel = ref(false)
  const onDeleteOrder = async (item: ConsultOrderItem) => {
    loadingDel.value = true
    try {
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      // 如果有这个函数 就调用这个函数
      callblack && callblack(item.id)
    } catch (err) {
      showFailToast('删除失败')
    } finally {
      loadingDel.value = false
    }
  }
  return { loadingDel, onDeleteOrder }
}

export const useOrderDetail = (id: string) => {
  const order = ref<OrderDetail>()
  const loading = ref(false)
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { loading, order }
}

// 发送验证码
export const useMobileCode = (mobile: Ref<string>, type: CodeType) => {
  const time = ref(0)
  const form = ref<FormInstance | null>(null)
  let timeId: number | undefined = undefined
  const send = async () => {
    if (time.value > 0) return
    // 校验mobile是否校验成功
    await form.value?.validate('mobile')
    // 上面校验成功后发验证码
    // console.log('ok')
    await sendMobileCode(mobile.value, type)
    time.value = 60

    // 开启倒计时
    if (timeId) clearInterval(timeId) //如果有计时器，那么我们先清除计时器
    timeId = setInterval(() => {
      if (time.value <= 0) clearInterval(timeId)
      time.value--
    }, 1000)
    // 组件销毁
    onUnmounted(() => {
      clearInterval(timeId)
    })
  }
  return { form, send, time }
}
